<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/CogDataSet">
		<xsl:for-each select="TableInfo/ColumnInfo[IsIndexed = 'true' or IsPrimaryKey = 'true' or IsForeignKey = 'true']">
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[<xsl:value-of select="../Schema"/>].[<xsl:value-of select="Table2"/>_LoadBy<xsl:value-of select="Column"/>]') AND type in (N'P', N'PC'))
DROP PROCEDURE [<xsl:value-of select="../Schema"/>].[<xsl:value-of select="Table2"/>_LoadBy<xsl:value-of select="Column"/>]
GO

CREATE PROCEDURE [<xsl:value-of select="../Schema"/>].[<xsl:value-of select="Table2"/>_LoadBy<xsl:value-of select="Column"/>]
(
	@<xsl:value-of select="Column"/><xsl:text> </xsl:text><xsl:value-of select="DataType"/>
)
AS
BEGIN
SELECT <xsl:for-each select="../ColumnInfo">[<xsl:value-of select="Column"/>]<xsl:if test="not(position()=last())">,
	</xsl:if></xsl:for-each>
FROM [<xsl:value-of select="../Schema"/>].[<xsl:value-of select="Table"/>_Select]
WHERE <xsl:value-of select="Column"/> = @<xsl:value-of select="Column"/>
END
GO
		</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>
